package com.cetcs.kmga.dataManager.controller;


import com.cetcs.kmga.common.BaseController;
import com.cetcs.kmga.common.BaseResponse;
import com.cetcs.kmga.common.Constant;
import com.cetcs.kmga.common.Data;
import com.cetcs.kmga.dataManager.annotation.LogAnnotation;
import com.cetcs.kmga.dataManager.entity.dataManager.vo.FlowRegularityLineCharVo;
import com.cetcs.kmga.dataManager.entity.dataManager.vo.RankOfBehaviorVo;
import com.cetcs.kmga.dataManager.entity.global.IdAndValVo;
import com.cetcs.kmga.dataManager.entity.global.LogLevelEnum;
import com.cetcs.kmga.dataManager.entity.global.LogTypeEnum;
import com.cetcs.kmga.dataManager.entity.global.NameAndValue;
import com.cetcs.kmga.dataManager.service.dataManager.UsePatternAnalysisService;
import com.cetcs.kmga.dataManager.util.CheckIpUtil;
import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.stream.Collectors;


/**
 * 数据监控.使用规律分析页面控制器
 *
 * @author zhancghunlin
 * @version V1.0 创建时间：2017-10-10 14:45
 *          Copyright 2017 by CETC
 */
@RestController
@RequestMapping("/usePatternAnalysis")
public class UsePatternAnalysisController extends BaseController {

    @Autowired
    private UsePatternAnalysisService usePatternAnalysisService;

    /*************
     **** 概览 ***
     ************/

    /************************************* yyangs start***************/
    /**
     * 资源访问量排名
     *
     * @return
     */
    @LogAnnotation(module = "数据监管-使用规律分析-概况", logType = LogTypeEnum.业务日志, description = "资源访问量排名", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/rankOfResourceAccess", method = RequestMethod.GET)
    public BaseResponse getRankOfResourceAccess(@RequestParam(value = "time") String time) {
        try {
            Data data = time2Data(time);
            List<RankOfBehaviorVo> result = usePatternAnalysisService.getRankOfResourceAccess(data);
            return this.ok(result);
        } catch (Exception e) {
            e.printStackTrace( );
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 每日访问量规律
     *
     * @return
     */
    @LogAnnotation(module = "数据监管-使用规律分析-概况", logType = LogTypeEnum.业务日志, description = "每日访问量规律", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/dailyTrafficPatterns", method = RequestMethod.GET)
    public BaseResponse getDailyTrafficPatterns(@RequestParam(value = "time") String time) {
        try {
            Data data = time2Data(time);
            data.put("starTime", data.get("starTime") + " 00:00:00");
            data.put("endTime", data.get("endTime") + " 23:59:59");
            List<NameAndValue> result = usePatternAnalysisService.getDailyTrafficPatterns(data);
            FlowRegularityLineCharVo flowRegularityLineCharVo = new FlowRegularityLineCharVo( );
            //封装x轴
            List<String> xAxis = Lists.newArrayList("0点", "1点", "2点", "3点", "4点", "5点", "6点", "7点", "8点", "9点", "10点", "11点", "12点", "13点", "14点", "15点", "16点", "17点", "18点", "19点", "20点", "21点", "22点", "23点");
            List<String> dataResule = Lists.newArrayList("0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0");

            for (NameAndValue nameAndValue : result) {
                Integer index = Integer.parseInt(nameAndValue.getName( ));
                if (index <= dataResule.size( )) {
                    dataResule.set(index, nameAndValue.getValue( ));
                }
            }
            flowRegularityLineCharVo.setData(dataResule);
            flowRegularityLineCharVo.setxAxis(xAxis);
            return this.ok(flowRegularityLineCharVo);
        } catch (Exception e) {
            e.printStackTrace( );
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 活跃IP统计（访问次数）
     *
     * @return
     */
    @LogAnnotation(module = "数据监管-使用规律分析-概况", logType = LogTypeEnum.业务日志, description = "活跃IP统计", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/activeIPStatistics", method = RequestMethod.GET)
    public BaseResponse getActiveIPStatistics(@RequestParam(value = "time") String time) {
        try {
            Data data = time2Data(time);
            List<RankOfBehaviorVo> result = usePatternAnalysisService.getActiveIPStatistics(data);
            return this.ok(result);
        } catch (Exception e) {
            e.printStackTrace( );
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }


    /**
     * IP来源分布
     *
     * @return
     */
    @LogAnnotation(module = "数据监管-使用规律分析-概况", logType = LogTypeEnum.业务日志, description = "IP来源分布", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/distributionOfIPSource", method = RequestMethod.GET)
    public BaseResponse getDistributionOfIPSource(@RequestParam(value = "time") String time) {
        try {
            Data data = time2Data(time);
            List<NameAndValue> result = usePatternAnalysisService.getDistributionOfIPSource(data);
            return this.ok(result);
        } catch (Exception e) {
            e.printStackTrace( );
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }


    /**
     * 操作类型分布
     *
     * @return
     */
    @LogAnnotation(module = "数据监管-使用规律分析-概况", logType = LogTypeEnum.业务日志, description = "操作类型分布", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/distributionOfOptType", method = RequestMethod.GET)
    public BaseResponse getDistributionOfOptType(@RequestParam(value = "time") String time) {
        try {
            Data data = time2Data(time);
            List<NameAndValue> result = usePatternAnalysisService.getDistributionOfOptType(data);
            return this.ok(result);
        } catch (Exception e) {
            e.printStackTrace( );
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }


    public Data time2Data(String time) {
        Data data = new Data( );
        data.put("starTime", time.substring(0, 10));
        data.put("endTime", time.substring(13, 23));
        return data;
    }

    /************************************* yyangs end ***************/


    /********************
     **** 资源使用规律 ***
     ********************/

    /**
     * 查询资源列表,查询资源ID和名称
     *
     * @return
     */
    @LogAnnotation(module = "数据监管-使用规律", logType = LogTypeEnum.业务日志, description = "查询资源列表", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/listResources", method = RequestMethod.GET)
    public BaseResponse listAllResources(@RequestParam(value = "keyword", required = false) String keyword) {
        try {
            List<IdAndValVo> result = usePatternAnalysisService.listAllResources(keyword);
            return this.ok(result);
        } catch (Exception e) {
            e.printStackTrace( );
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 资源概况查询,根据id获得资源信息
     *
     * @return
     */
    @LogAnnotation(module = "数据监管-使用规律", logType = LogTypeEnum.业务日志, description = "资源概况查询", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/getResourceById", method = RequestMethod.GET)
    public BaseResponse getResourceById(@RequestParam(value = "id", required = false) String id, @RequestParam(value = "keyTime",required = false) String keyTime) {
        try {
            if (StringUtils.isEmpty(id)) {
                return this.error(Constant.SYSTEM_EXCEPTION, "资源ID不能为空");
            }
            return this.ok(usePatternAnalysisService.getResourceById(id, keyTime));
        } catch (Exception e) {
            e.printStackTrace( );
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 库表访问排名
     */
    @LogAnnotation(module = "数据监管-使用规律", logType = LogTypeEnum.业务日志, description = "库表访问排名", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/getTableTraffic", method = RequestMethod.GET)
    public BaseResponse getTableTrafficByResourceId(@RequestParam(value = "id", required = false) String resourceId,@RequestParam(value = "keyTime",required = false) String keyTime) {
        try {
            if (StringUtils.isEmpty(resourceId)) {
                return this.error(Constant.SYSTEM_EXCEPTION, "资源ID不能为空");
            }
            return this.ok(usePatternAnalysisService.getTableTraffic(resourceId, keyTime));
        } catch (Exception e) {
            e.printStackTrace( );
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    /**
     * IP排名,数据库账户排名,操作类型排名,敏感数据访问排名
     */
    @LogAnnotation(module = "数据监管-使用规律", logType = LogTypeEnum.业务日志, description = "IP排名,数据库账户排名,操作类型排名,敏感数据访问排名", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/findIpAccessStatistic", method = RequestMethod.GET)
    public BaseResponse findIpAccessStatistic(@RequestParam(value = "id", required = false) String resourceId,@RequestParam(value = "keyTime",required = false) String keyTime) {
        try {
            if (StringUtils.isEmpty(resourceId)) {
                return this.error(Constant.SYSTEM_EXCEPTION, "资源ID不能为空");
            }
            return this.ok(usePatternAnalysisService.findIpAccessStatistic(resourceId, keyTime));
        } catch (Exception e) {
            e.printStackTrace( );
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 每日流量规律 查询
     */
    @LogAnnotation(module = "数据监管-使用规律", logType = LogTypeEnum.业务日志, description = "每日流量规律", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/findTrafficStatisticOfDay", method = RequestMethod.GET)
    public BaseResponse findTrafficStatisticOfDay(@RequestParam(value = "id", required = false) String resourceId,@RequestParam(value = "keyTime",required = false) String keyTime) {
        try {
            if (StringUtils.isEmpty(resourceId)) {
                return this.error(Constant.SYSTEM_EXCEPTION, "资源ID不能为空");
            }
            return this.ok(usePatternAnalysisService.findTrafficStatisticOfDay(resourceId, keyTime));
        } catch (Exception e) {
            e.printStackTrace( );
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }


    /*************yyangs start**************/


    /*****************
     * 用户行为规律****
     *****************/

    /**
     * 分析对象列表
     */
    @LogAnnotation(module = "数据监管-使用规律-用户行为规律", logType = LogTypeEnum.业务日志, description = "分析对象列表", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/getAnalysisObjectLists", method = RequestMethod.GET)
    public BaseResponse getAnalysisObjectLists() {
        try {
            Data dataMap = new Data( );
            return this.ok(usePatternAnalysisService.getAnalysisObjectLists(dataMap));
        } catch (Exception e) {
            e.printStackTrace( );
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 访问流量与访问次数
     */
    @LogAnnotation(module = "数据监管-使用规律-用户行为规律", logType = LogTypeEnum.业务日志, description = "访问流量与访问次数", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/getAccessTrafficAndVisits", method = RequestMethod.GET)
    public BaseResponse getAccessTrafficAndVisits(@RequestParam(value = "typeName", required = false) String typeName,
                                                  @RequestParam(value = "keyTime", required = false) String keyTime) {
        try {
            Data dataMap = new Data( );
            dataMap.put("resourceId", typeName);
            if (keyTime == null) {
                //如果时间为空
                dataMap.put("keyTime", keyTime);
            } else {
                dataMap.put("keyTime", keyTime);
            }
            return this.ok(usePatternAnalysisService.getAccessTrafficAndVisits(dataMap));
        } catch (Exception e) {
            e.printStackTrace( );
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }


    /**
     * 访问资源分析 排名char图
     */
    @LogAnnotation(module = "数据监管-使用规律-用户行为规律", logType = LogTypeEnum.业务日志, description = "访问资源分析排名char图", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/getAccessResourceAnalysisForChar", method = RequestMethod.GET)
    public BaseResponse getAccessResourceAnalysisForChar(@RequestParam(value = "keyType", required = false) Integer keyType,
                                                         @RequestParam(value = "keyVal", required = false) String keyVal,
                                                         @RequestParam(value = "keyTime", required = false) String keyTime) {
        try {
            String cid = null;
            String ip = null;
            if (keyType == 1) {
                cid = keyVal;
            } else if (keyType == 2) {
                if (CheckIpUtil.isIpValid(keyVal)) {
                    ip = keyVal;
                } else {
                    return this.error(Constant.SYSTEM_EXCEPTION, "输入的IP地址格式不正确！");
                }
            }
            return this.ok(usePatternAnalysisService.getAccessResourceAnalysisForChar(cid,ip,keyTime));
        } catch (Exception e) {
            e.printStackTrace( );
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }


    /**
     * 访问资源分析 每日流量规律
     */
    @LogAnnotation(module = "数据监管-使用规律-用户行为规律", logType = LogTypeEnum.业务日志, description = "访问资源分析每日流量规律", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/getAccessResourceAnalysisForDailyTrafficRules", method = RequestMethod.GET)
    public BaseResponse getAccessResourceAnalysisForDailyTrafficRules(@RequestParam(value = "typeName", required = false) String typeName,
                                                                      @RequestParam(value = "keyTime", required = false) String keyTime) {
        try {
            Data dataMap = new Data( );
            dataMap.put("resourceId", typeName);
            if (keyTime == null) {
                //如果时间为空
                dataMap.put("keyTime", keyTime);
            } else {
                dataMap.put("keyTime", keyTime);
            }
            return this.ok(usePatternAnalysisService.getAccessResourceAnalysisForDailyTrafficRules(dataMap));
        } catch (Exception e) {
            e.printStackTrace( );
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 活跃情况分析 排名char图
     */
    @LogAnnotation(module = "数据监管-使用规律-用户行为规律", logType = LogTypeEnum.业务日志, description = "活跃情况分析排名char图", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/getActiveAnalysisForChar", method = RequestMethod.GET)
    public BaseResponse getActiveAnalysisForChar(@RequestParam(value = "typeName", required = false) String typeName,
                                                 @RequestParam(value = "keyTime", required = false) String keyTime) {
        try {
            Data dataMap = new Data( );
            dataMap.put("resourceId", typeName);
            if (keyTime == null) {
                //如果时间为空
                dataMap.put("keyTime", keyTime);
            } else {
                dataMap.put("keyTime", keyTime);
            }
            return this.ok(usePatternAnalysisService.getActiveAnalysisForChar(dataMap));
        } catch (Exception e) {
            e.printStackTrace( );
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }


    /**
     * 活跃情况分析 每日流量规律
     */
    @LogAnnotation(module = "数据监管-使用规律-用户行为规律", logType = LogTypeEnum.业务日志, description = "活跃情况分析每日流量规律", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/getActiveAnalysisForDailyTrafficRules", method = RequestMethod.GET)
    public BaseResponse getActiveAnalysisForDailyTrafficRules(@RequestParam(value = "keyType", required = false) Integer keyType,
                                                              @RequestParam(value = "keyVal", required = false) String keyVal,
                                                              @RequestParam(value = "keyTime", required = false) String keyTime) {
        try {
            String cid = null;
            String ip = null;
            if (keyType == 1) {
                cid = keyVal;
            } else if (keyType == 2) {
                if (CheckIpUtil.isIpValid(keyVal)) {
                    ip = keyVal;
                } else {
                    return this.error(Constant.SYSTEM_EXCEPTION, "输入的IP地址格式不正确！");
                }
            }
            return this.ok(usePatternAnalysisService.getActiveAnalysisForDailyTrafficRules(cid,ip,keyTime));
        } catch (Exception e) {
            e.printStackTrace( );
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }


    /*************yyangs end  **************/


}
